package com.oms.web.contronller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oms.domain.User;
import com.oms.service.AcknowledgementService;
import com.oms.service.MessageService;

@Controller
public class MessageController {
	
	@Resource
	private MessageService messageService;
	
	@Resource
	private AcknowledgementService acknowledgementService;

	/**
	 * 查看接收到的消息列表
	 */
	//显示用户收到的消息页面
	@RequestMapping("/message/receive/list")
	public String listReceiveMsg(HttpServletRequest request,HttpServletResponse response){
		User user=(User) request.getSession().getAttribute("user");	//获取当前用户
		//用户是否已经登录
		if(user==null){
			request.setAttribute("failTitle", "系统发生异常!");
			response.setHeader("REFRESH", "10;URL=/organizationmanagement/index.jsp");
			return "fail";
		}
		request.setAttribute("receiveAckList", acknowledgementService.getMessageListWithPage(user, 0, 20,null,null));
//		request.setAttribute("sendMessageList",messageService.getMessageListWithPage(user, 0, 20, null, null) );
//		System.out.println("————————————————————————————————————传送结果到页面____________________________");
		return "msg/msgReceivelist";
	}
	
	/**
	 * 查看接收到的消息列表
	 */
	//显示用户收到的消息页面
	@RequestMapping("/message/receive/list/unread")
	public String getUnReadMsgListWithPage(HttpServletRequest request,HttpServletResponse response){
		User user=(User) request.getSession().getAttribute("user");	//获取当前用户
		//用户是否已经登录
		if(user==null){
			request.setAttribute("failTitle", "系统发生异常!");
			response.setHeader("REFRESH", "10;URL=/organizationmanagement/index.jsp");
			return "fail";
		}
		request.setAttribute("receiveAckList", acknowledgementService.getUnReadMsgListWithPage(user, 0, 20,null,null));
		return "msg/msgReceivelist";
	}
	
	/**
	 * 查看接收到的消息列表
	 */
	//显示用户收到的消息页面
	@RequestMapping("/message/receive/list/readed")
	public String getReadedMsgListWithPage(HttpServletRequest request,HttpServletResponse response){
		User user=(User) request.getSession().getAttribute("user");	//获取当前用户
		//用户是否已经登录
		if(user==null){
			request.setAttribute("failTitle", "系统发生异常!");
			response.setHeader("REFRESH", "10;URL=/organizationmanagement/index.jsp");
			return "fail";
		}
		request.setAttribute("receiveAckList", acknowledgementService.getReadedMsgListWithPage(user, 0, 20,null,null));
		return "msg/msgReceivelist";
	}
	
	/**
	 * rest方式查看一条接收到的信息的详情
	 */
	@RequestMapping(value="/message/receive/{id}",method=RequestMethod.GET)
	public String receiverReadMsg(@PathVariable("id") Integer id,HttpServletRequest request,HttpServletResponse response){
		User user=(User) request.getSession().getAttribute("user");	//得到当前用户
		try{
			request.setAttribute("acknowledgement", acknowledgementService.receiverReadMsg(id, user));	//读信息业务
		}catch(RuntimeException e){
			//没有权限，异常操作
			if(e.getMessage().equals("1")){	
				request.setAttribute("failTitle", "异常操作!");
				response.setHeader("REFRESH", "10;URL=/organizationmanagement/index.jsp");
				return "fail";
			}
			request.setAttribute("failTitle", "系统发生异常!");
			request.setAttribute("failContent", "10秒后跳转到主页");
			response.setHeader("REFRESH", "10;URL=/organizationmanagement/index.jsp");
			return "fail";
		}
		return "msg/receiveMsgDetails";
	}
	
	
	/**
	 * rest方式删除一条接收到的信息
	 */
	@RequestMapping(value="/message/receive/{id}",method=RequestMethod.DELETE)
	public String receiverDeleteMsg(@PathVariable("id") Integer id,HttpServletRequest request,HttpServletResponse response){
		User user=(User) request.getSession().getAttribute("user");	//获取该用户的登录信息
		System.out.println(request.getMethod());
		try{
			acknowledgementService.deleteMsg(id, user);
		}catch(RuntimeException e){
			request.setAttribute("failTitle", "系统发生异常!");
			request.setAttribute("failContent", "10秒后跳转到主页");
			response.setHeader("REFRESH", "10;URL=/organizationmanagement/index.jsp");
			return "fail";
		}
		return "redirect:/message/receive/list.do";
	}

	
	//显示用户收到的消息页面
	@RequestMapping("/message/receive/unreadNum")
	@ResponseBody
	public Integer getUnreadNum(HttpServletRequest request,HttpServletResponse response){
		User user=(User) request.getSession().getAttribute("user");	//获取当前用户
		//用户是否已经登录
		if(user==null){
			return null;
		}
		Integer unreadNum=acknowledgementService.getUnReadMsgCount(user);
		return unreadNum;
	}
	
	/**
	 * ________________________________________________发送消息的操作____________________________________________________________
	 */
	
	/**
	 * 查看发送的消息列表
	 */
	@RequestMapping("/message/send/list")
	public String listSendMsg(HttpServletRequest request,HttpServletResponse response){
		User user=(User) request.getSession().getAttribute("user");	//获取当前用户
		//用户是否已经登录
		if(user==null){
			request.setAttribute("failTitle", "系统发生异常!");
			response.setHeader("REFRESH", "10;URL=/organizationmanagement/index.jsp");
			return "fail";
		}
		request.setAttribute("sendMessageList",messageService.getMessageListWithPage(user, 0, 20, null, null) );
		return "msg/msgSendlist";
	}
	
	/**
	 * rest方式查看一条已发送的信息的详情
	 */
	@RequestMapping(value="/message/send/{id}",method=RequestMethod.GET)
	public String senderReadMsg(@PathVariable("id") Integer id,HttpServletRequest request,HttpServletResponse response){
		User user=(User) request.getSession().getAttribute("user");	//获取当前登录用户
		try{
			request.setAttribute("message", messageService.senderReadMsg(id, user));	//阅读业务，返回的信息传给页面
		}catch(RuntimeException e){
			if(e.getMessage().equals("1")){
				request.setAttribute("failTitle", "异常操作!");
				response.setHeader("REFRESH", "10;URL=/organizationmanagement/index.jsp");
				return "fail";
			}
			request.setAttribute("failTitle", "系统发生异常!");
			request.setAttribute("failContent", "10秒后跳转到主页");
			response.setHeader("REFRESH", "10;URL=/organizationmanagement/index.jsp");
			return "fail";
		}
		return "msg/sendMsgDetails";
	}
	
	/**
	 * rest方式删除一条已发送的信息
	 */
	@RequestMapping(value="/message/send/{id}",method=RequestMethod.DELETE)
	public String senderDeleteMsg(@PathVariable("id") Integer id,HttpServletRequest request,HttpServletResponse response){
		User user=(User) request.getSession().getAttribute("user");	//获取该用户的登录信息
		System.out.println(request.getMethod());
		try{
			messageService.deleteMsg(id, user);
		}catch(RuntimeException e){
			request.setAttribute("failTitle", "系统发生异常!");
			request.setAttribute("failContent", "10秒后跳转到主页");
			response.setHeader("REFRESH", "10;URL=/organizationmanagement/index.jsp");
			return "fail";
		}
		return "redirect:/message/send/list.do";
	}
}
